<template>
	<view class="product-manage">
		<view class="product-manage-tabs">
			<tabs :list="tabs" v-model="active" :flex="true" @input="select"></tabs>
		</view>
		<scroll-view class="product-manage-view" scroll-y @scrolltolower="loadMore">
			<view class="product-manage-list" v-if="productList.length">
				<view class="product-manage-item" v-for="(data,index) in productList" :key="index" @click="goEdit(data.store_goods_id)">
					<image class="product-img" :src="data.image_middle" mode="aspectFill"></image>
					<view class="product-manage-info">
						<view class="product-manage-info-tit">{{data.store_goods_name}}</view>
						<view class="product-manage-info-text">市场价：¥{{data.market_price}}</view>
						<view class="product-manage-info-text">零售价：¥{{data.retail_price}}</view>
						<view class="product-manage-info-text">成本价：¥{{data.factory_price}}</view>
						<view class="product-manage-info-text"><text>总销量：<text>{{data.sell_number}}</text></text><text>库存：<text>{{data.num}}</text></text></view>
					</view>
					<image src="https://master.shengejiaju.cn/web/static/icons/ico_skip_mine@2x.png" class="product-arrow"></image>
				</view>
			</view>
			<nothing :text="tips" :btnTxt="'去添加'" @goUrl="add" v-else></nothing>
			<no-more :text="'数据加载完毕'" v-if="productList.length && noMore"></no-more>
		</scroll-view>
		<view class="product-manage-bottom">
			<view @click="add()">添加{{is_package?'套餐':'商品'}}</view>
			<view @click="goCategory" v-if="!isCategory">分类管理</view>
			<view @click="batchManage" v-if="productList.length">批量管理</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				active: 0,
				isRefresh: false,
				productList: [],
				tabs: [{
					title: '出售中'
				},{
					title: '已下架'
				}],
				categoryID: '',
				isCategory: false,
				is_package: false,
				tips: '还未添加商品',
				page: 0,
				noMore: false
			};
		},
		onLoad(options) {
			if(options.type){
				this.categoryID = options.type
				this.isCategory = true
			}
			if(options.is_package){
				this.is_package = true
				uni.setNavigationBarTitle({
					title: '套餐管理'
				})
				this.tips = '还未添加套餐'
			}else{
				this.is_package = false
				uni.setNavigationBarTitle({
					title: '商品管理'
				})
				this.tips = '还未添加商品'
			}
		},
		onShow() {
			this.productList = []
			this.noMore = false
			this.page = 0
			this.init()
			this.active = 0
		},
		methods: {
			loadMore() {
				if(!this.noMore){
					this.page++
					this.init(this.page,this.active+1)
				}
			},
			init(page,issale){
				let data = {
					limit: 10,
					page: page?page:0,
					order_by: 'add_time',
					asc: 1,
					store_goods_id: '',
					store_goods_sn: '',
					user_id:  JSON.parse(uni.getStorageSync('userInfo')).user_id,
					class_id: this.categoryID,
					is_sale: issale?issale:1,
					goods_type: '',
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
					paramter: '',
					type: this.is_package?2:1
				}
				this.$api.StoreGoodsList(data).then(res => {
					if(res.datas.length){
						res.datas.forEach(item => {
							this.productList.push(item)
						})
					}else{
						this.noMore = true
					}
				})
			},
			select(e){
				this.noMore = false
				this.productList = []
				this.page = 0
				this.init(0,e+1)
			},
			goCategory(){
				if(this.is_package){
					uni.navigateTo({
						url: "/pages/product-manage/product-category?is_package=1"
					})
				}else{
					uni.navigateTo({
						url: "/pages/product-manage/product-category"
					})
				}
			},
			batchManage(){
				if(this.categoryID){
					if(this.is_package){
						uni.navigateTo({
							url: "/pages/product-manage/batch-manage?type="+this.active + '&categoryID=' + this.categoryID + '&is_package=1'
						})
					}else{
						uni.navigateTo({
							url: "/pages/product-manage/batch-manage?type="+this.active + '&categoryID=' + this.categoryID
						})
					}
				}else{
					if(this.is_package){
						uni.navigateTo({
							url: "/pages/product-manage/batch-manage?type="+this.active + '&is_package=1'
						})
					}else{
						uni.navigateTo({
							url: "/pages/product-manage/batch-manage?type="+this.active
						})
					}
				}
			},
			add(){
				if(this.categoryID){
					if(this.is_package){
						uni.navigateTo({
							url: "/pages/product-manage/add-product?class_id=" + this.categoryID + '&is_package=1'
						})
					}else{
						uni.navigateTo({
							url: "/pages/product-manage/add-product?class_id=" + this.categoryID
						})
					}
				}else{
					if(this.is_package){
						uni.navigateTo({
							url: "/pages/product-manage/add-product?is_package=1"
						})
					}else{
						uni.navigateTo({
							url: "/pages/product-manage/add-product"
						})
					}
				}
			},
			goEdit(id){
				if(this.is_package){
					uni.navigateTo({
						url: '/pages/product-manage/add-product?id='+id + '&is_package=1'
					})
				}else{
					uni.navigateTo({
						url: '/pages/product-manage/add-product?id='+id
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/product-manage.scss'
</style>